package com.aikg.kgrag_java.pojo.entity.neo4j;

/**
 * @ClassName: Node
 * @Description: TODO
 * @Author: 李宗雨
 * @Date: 2025/1/4 12:45
 * @Version: 1.0
 **/
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.neo4j.core.schema.DynamicLabels;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Property;
import org.springframework.data.neo4j.core.schema.*;

import java.io.Serializable;
import java.util.*;

@Getter
@Setter
@org.springframework.data.neo4j.core.schema.Node(value = "node")
public class Node implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    @Property("projectId")
    private Long projectId;

    @Property("name")
    private String name;

    @Property("tag")
    private String tag;

    @DynamicLabels
    private Set<String> labels = new HashSet<String>();

    @Property("enName")
    private String enName;

    @Property("description")
    private String description;

    @Property("rank")
    private Integer rank;

    @CompositeProperty
    private Map<String, String> property = new HashMap<>();

//    /**
//     * 节点作为关系中文头节点指向的尾节点
//     */
//    @Relationship(type = "RELATION", direction = Relationship.Direction.OUTGOING)
//    private List<NodeRelation> target = new ArrayList<>();
//
//    /**
//     * 节点作为尾节点来自的头节点
//     */
//    @Relationship(type = "RELATION", direction = Relationship.Direction.INCOMING)
//    private List<NodeRelation> source = new ArrayList<>();

    public void updateLabels(Set<String> newLabels) {
        this.labels.addAll(newLabels);
    }

    @Override
    public String toString() {
        return "Node{" +
                "id=" + id +
                ", projectId=" + projectId +
                ", name='" + name + '\'' +
                ", tag='" + tag + '\'' +
                ", labels=" + labels +
                ", enName='" + enName + '\'' +
                ", description='" + description + '\'' +
                ", property=" + property +
                '}';
    }
    public String toString4Me(){
        // 若啥实体名称和属性都没有则直接返回空字符串
        if (this.name.equals("null") && this.property.isEmpty()){
            return "";
        }
        StringBuilder result = new StringBuilder();
        result.append("{");

        if (!this.name.equals("null")){
            result.append("\"实体名\":").append("\"").append(this.name).append("\"");
        }
        if(!this.property.isEmpty()){
            int a = 0;
            for (String key: this.property.keySet()) {
                String value = this.property.get(key);
                if(!value.isEmpty() && !value.equals("null") && !value.equals("undefined")){
                    if(a == 0){
                        result.append(",\"属性\":{");
                        a++;
                    }
                    result.append("\"").append(key).append("\"")
                            .append(":")
                            .append("\"").append(value).append("\"")
                            .append(",");
                }
            }
            if(a != 0){
                result.deleteCharAt(result.length() - 1);
                result.append("}");
            }
        }

        result.append("}");
        return result.toString();
    }


    public String toString4Front(){
        return "";
    }

    /**
     * 添加单个节点标签
     *
     * @param newLabel
     */
    public void addLabel(String newLabel) {
        this.labels.add(newLabel);
    }

    /**
     * 添加多个节点标签
     *
     * @param newLabels
     */
    public void addLabels(Set<String> newLabels) {
        this.labels.addAll(newLabels);
    }

    /**
     * 添加/修改节点属性
     *
     * @param key
     * @param value
     */
    public void addProperty(String key, String value) {
        this.property.put(key, value);
    }


    /**
     * 添加关系目标节点
     *
     * @param nodeRelation
     */
//    public void addOutRelation(NodeRelation nodeRelation) {
//        this.target.add(nodeRelation);
//    }

    /**
     * 添加关系来源节点
     *
     * @param nodeRelation
     */
//    public void addIncomeRelation(NodeRelation nodeRelation) {
//        this.source.add(nodeRelation);
//    }

    /**
     * 判断节点是否是本体
     * @return
     */
    public boolean isOntology(){
        return this.labels.contains("ontology");
    }

    /**
     * 判断节点是否是实体
     * @return
     */
    public boolean isEntity(){
        return this.labels.contains("entity");
    }

    /**
     * 判断节点是否是Thing节点
     * @return
     */
    public boolean isThing(){
        return this.labels.contains("Thing");
    }
}